home *** CD-ROM | disk | FTP | other *** search
/ CU Amiga Super CD-ROM 15 / CU Amiga Magazine's Super CD-ROM 15 (1997)(EMAP Images)(GB)[!][issue 1997-10].iso / CUCD / Graphics / Ghostscript / source / gspcolor.c < prev    next >
Encoding:
C/C++ Source or Header  |  1997-04-25  |  24.8 KB  |  782 lines

  1. /* Copyright (C) 1993, 1995, 1996, 1997 Aladdin Enterprises.  All rights reserved.
  2.   
  3.   This file is part of Aladdin Ghostscript.
  4.   
  5.   Aladdin Ghostscript is distributed with NO WARRANTY OF ANY KIND.  No author
  6.   or distributor accepts any responsibility for the consequences of using it,
  7.   or for whether it serves any particular purpose or works at all, unless he
  8.   or she says so in writing.  Refer to the Aladdin Ghostscript Free Public
  9.   License (the "License") for full details.
  10.   
  11.   Every copy of Aladdin Ghostscript must include a copy of the License,
  12.   normally in a plain ASCII text file named PUBLIC.  The License grants you
  13.   the right to copy, modify and redistribute Aladdin Ghostscript, but only
  14.   under certain conditions described in the License.  Among other things, the
  15.   License requires that the copyright notice and this notice be preserved on
  16.   all copies.
  17. */
  18.  
  19. /* gspcolor.c */
  20. /* Pattern color operators and procedures for Ghostscript library */
  21. #include "math_.h"
  22. #include "gx.h"
  23. #include "gserrors.h"
  24. #include "gsrop.h"
  25. #include "gsstruct.h"
  26. #include "gsutil.h"            /* for gs_next_ids */
  27. #include "gxarith.h"
  28. #include "gxfixed.h"
  29. #include "gxmatrix.h"
  30. #include "gxcoord.h"            /* for gs_concat, gx_tr'_to_fixed */
  31. #include "gxcspace.h"            /* for gscolor2.h */
  32. #include "gxcolor2.h"
  33. #include "gxdcolor.h"
  34. #include "gxdevice.h"
  35. #include "gxdevmem.h"
  36. #include "gxclip2.h"
  37. #include "gspath.h"
  38. #include "gxpath.h"
  39. #include "gxpcolor.h"
  40. #include "gzstate.h"
  41. #include "gsimage.h"
  42.  
  43. private_st_client_pattern();
  44. public_st_pattern_instance();
  45.  
  46. /* Import the Pattern reloading procedure from gxpcmap.c. */
  47. int gx_pattern_load(P4(gx_device_color *, const gs_imager_state *,
  48.                gx_device *, gs_color_select_t));
  49.  
  50. /* Define the Pattern color space. */
  51. extern cs_proc_remap_color(gx_remap_Pattern);
  52. private cs_proc_install_cspace(gx_install_Pattern);
  53. private cs_proc_adjust_cspace_count(gx_adjust_cspace_Pattern);
  54. private cs_proc_init_color(gx_init_Pattern);
  55. private cs_proc_adjust_color_count(gx_adjust_color_Pattern);
  56. private struct_proc_enum_ptrs(gx_enum_ptrs_Pattern);
  57. private struct_proc_reloc_ptrs(gx_reloc_ptrs_Pattern);
  58. const gs_color_space_type
  59.     gs_color_space_type_Pattern =
  60.      { gs_color_space_index_Pattern, -1, false,
  61.        gx_init_Pattern, gx_no_concrete_space,
  62.        gx_no_concretize_color, NULL,
  63.        gx_remap_Pattern, gx_install_Pattern,
  64.        gx_adjust_cspace_Pattern, gx_adjust_color_Pattern,
  65.        gx_enum_ptrs_Pattern, gx_reloc_ptrs_Pattern
  66.      };
  67.  
  68. /* makepattern */
  69. private int compute_inst_matrix(P3(gs_pattern_instance *pinst,
  70.                    const gs_state *saved,
  71.                    gs_rect *pbbox));
  72. int
  73. gs_makepattern(gs_client_color *pcc, const gs_client_pattern *pcp,
  74.   const gs_matrix *pmat, gs_state *pgs, gs_memory_t *mem)
  75. {    gs_pattern_instance inst;
  76.     gs_pattern_instance *pinst;
  77.     gs_state *saved;
  78.     gs_rect bbox;
  79.     gs_fixed_rect cbox;
  80.     int code;
  81.  
  82.     if ( mem == 0 )
  83.       mem = pgs->memory;
  84.     rc_alloc_struct_1(pinst, gs_pattern_instance, &st_pattern_instance,
  85.               mem, return_error(gs_error_VMerror),
  86.               "gs_makepattern");
  87.     inst.rc = pinst->rc;
  88.     saved = gs_state_copy(pgs, mem);
  89.     if ( saved == 0 )
  90.       { code = gs_note_error(gs_error_VMerror);
  91.         goto finst;
  92.       }
  93.     gs_concat(saved, pmat);
  94.     gs_newpath(saved);
  95.     gs_set_logical_op(saved, lop_default);
  96.     inst.template = *pcp;
  97.     inst.saved = saved;
  98.     code = compute_inst_matrix(&inst, saved, &bbox);
  99.     if ( code < 0 )
  100.       goto fsaved;
  101. #define mat inst.step_matrix
  102.     if_debug6('t', "[t]step_matrix=[%g %g %g %g %g %g]\n",
  103.           mat.xx, mat.xy, mat.yx, mat.yy, mat.tx, mat.ty);
  104.     /* Check for singular stepping matrix. */
  105.     if ( fabs(mat.xx * mat.yy - mat.xy * mat.yx) < 1.0e-6 )
  106.       { code = gs_note_error(gs_error_rangecheck);
  107.         goto fsaved;
  108.       }
  109.     if_debug4('t', "[t]bbox=(%g,%g),(%g,%g)\n",
  110.           bbox.p.x, bbox.p.y, bbox.q.x, bbox.q.y);
  111.     {    float bbw = bbox.q.x - bbox.p.x;
  112.         float bbh = bbox.q.y - bbox.p.y;
  113.         /* If the step and the size agree to within 1/2 pixel, */
  114.         /* make them the same. */
  115.         inst.size.x = (int)(bbw + 0.8);    /* 0.8 is arbitrary */
  116.         inst.size.y = (int)(bbh + 0.8);
  117.         if ( mat.xy == 0 && mat.yx == 0 &&
  118.              fabs(fabs(mat.xx) - bbw) < 0.5 &&
  119.              fabs(fabs(mat.yy) - bbh) < 0.5
  120.            )
  121.           {    gs_scale(saved, fabs(inst.size.x / mat.xx),
  122.                  fabs(inst.size.y / mat.yy));
  123.             code = compute_inst_matrix(&inst, saved, &bbox);
  124.             if ( code < 0 )
  125.               goto fsaved;
  126.             if_debug2('t',
  127.                 "[t]adjusted XStep & YStep to size=(%d,%d)\n",
  128.                 inst.size.x, inst.size.y);
  129.             if_debug4('t', "[t]bbox=(%g,%g),(%g,%g)\n",
  130.                   bbox.p.x, bbox.p.y, bbox.q.x, bbox.q.y);
  131.           }
  132.     }
  133.     if ( (code = gs_bbox_transform_inverse(&bbox, &mat, &inst.bbox)) < 0 )
  134.       goto fsaved;
  135.     if_debug4('t', "[t]ibbox=(%g,%g),(%g,%g)\n",
  136.           inst.bbox.p.x, inst.bbox.p.y, inst.bbox.q.x, inst.bbox.q.y);
  137.     inst.is_simple = (fabs(mat.xx) == inst.size.x && mat.xy == 0 &&
  138.               mat.yx == 0 && fabs(mat.yy) == inst.size.y);
  139.     if_debug6('t',
  140.           "[t]is_simple? xstep=(%g,%g) ystep=(%g,%g) size=(%d,%d)\n",
  141.           inst.step_matrix.xx, inst.step_matrix.xy,
  142.           inst.step_matrix.yx, inst.step_matrix.yy,
  143.           inst.size.x, inst.size.y);
  144.     gx_translate_to_fixed(saved, float2fixed(mat.tx - bbox.p.x),
  145.                   float2fixed(mat.ty - bbox.p.y));
  146.     mat.tx = bbox.p.x;
  147.     mat.ty = bbox.p.y;
  148. #undef mat
  149.     cbox.p.x = fixed_0;
  150.     cbox.p.y = fixed_0;
  151.     cbox.q.x = int2fixed(inst.size.x);
  152.     cbox.q.y = int2fixed(inst.size.y);
  153.     code = gx_clip_to_rectangle(saved, &cbox);
  154.     if ( code < 0 )
  155.       goto fsaved;
  156.     inst.id = gs_next_ids(1);
  157.     *pinst = inst;
  158.     pcc->pattern = pinst;
  159.     return 0;
  160. #undef mat
  161. fsaved:    gs_state_free(saved);
  162. finst:    gs_free_object(mem, pinst, "gs_makepattern");
  163.     return code;
  164. }
  165. /* Compute the stepping matrix and device space instance bounding box */
  166. /* from the step values and the saved matrix. */
  167. private int
  168. compute_inst_matrix(gs_pattern_instance *pinst, const gs_state *saved,
  169.   gs_rect *pbbox)
  170. {    double xx = pinst->template.XStep * saved->ctm.xx;
  171.     double xy = pinst->template.XStep * saved->ctm.xy;
  172.     double yx = pinst->template.YStep * saved->ctm.yx;
  173.     double yy = pinst->template.YStep * saved->ctm.yy;
  174.  
  175.     /* Adjust the stepping matrix so all coefficients are >= 0. */
  176.     if ( xx == 0 || yy == 0 )
  177.       { /* We know that both xy and yx are non-zero. */
  178.         double temp;
  179.         temp = xx, xx = yx, yx = temp;
  180.         temp = xy, xy = yy, yy = temp;
  181.       }
  182.     if ( xx < 0 )
  183.       xx = -xx, xy = -xy;
  184.     if ( yy < 0 )
  185.       yx = -yx, yy = -yy;
  186.     /* Now xx > 0, yy > 0. */
  187.     pinst->step_matrix.xx = xx;
  188.     pinst->step_matrix.xy = xy;
  189.     pinst->step_matrix.yx = yx;
  190.     pinst->step_matrix.yy = yy;
  191.     pinst->step_matrix.tx = saved->ctm.tx;
  192.     pinst->step_matrix.ty = saved->ctm.ty;
  193.     return gs_bbox_transform(&pinst->template.BBox, &ctm_only(saved),
  194.                  pbbox);
  195. }
  196.  
  197. /* setpattern */
  198. int
  199. gs_setpattern(gs_state *pgs, const gs_client_color *pcc)
  200. {    int code = gs_setpatternspace(pgs);
  201.     if ( code < 0 )
  202.         return code;
  203.     return gs_setcolor(pgs, pcc);
  204. }
  205.  
  206. /* setpatternspace */
  207. /* This does all the work of setpattern except for the final setcolor. */
  208. int
  209. gs_setpatternspace(gs_state *pgs)
  210. {    int code = 0;
  211.     if ( pgs->color_space->type->index != gs_color_space_index_Pattern )
  212.     {    gs_color_space cs;
  213.         cs.params.pattern.base_space =
  214.             *(gs_paint_color_space *)pgs->color_space;
  215.         cs.params.pattern.has_base_space = true;
  216.         cs.type = &gs_color_space_type_Pattern;
  217.         code = gs_setcolorspace(pgs, &cs);
  218.     }
  219.     return code;
  220. }
  221.  
  222. /* getpattern */
  223. /* This is only intended for the benefit of pattern PaintProcs. */
  224. const gs_client_pattern *
  225. gs_getpattern(const gs_client_color *pcc)
  226. {    return &pcc->pattern->template;
  227. }
  228.  
  229. /* makebitmappattern */
  230. private int bitmap_PaintProc(P2(const gs_client_color *, gs_state *));
  231. int
  232. gs_makebitmappattern(gs_client_color *pcc, const gx_tile_bitmap *tile,
  233.   bool mask, gs_state *pgs, gs_memory_t *mem)
  234. {    gs_client_pattern pat;
  235.     gs_matrix mat, smat;
  236.  
  237.     if ( tile->raster != bitmap_raster(tile->size.x) )
  238.       return_error(gs_error_rangecheck);
  239.     uid_set_UniqueID(&pat.uid, gs_next_ids(1));
  240.     pat.PaintType = (mask ? 2 : 1);
  241.     pat.TilingType = 1;
  242.     pat.BBox.p.x = 0;
  243.     pat.BBox.p.y = 0;
  244.     pat.BBox.q.x = tile->size.x;
  245.     pat.BBox.q.y = tile->rep_height;
  246.     pat.XStep = tile->size.x;
  247.     pat.YStep = tile->rep_height;
  248.     pat.PaintProc = bitmap_PaintProc;
  249.     pat.client_data = tile->data;
  250.     gs_make_identity(&mat);
  251.     gs_currentmatrix(pgs, &smat);
  252.     if ( smat.yy > 0 )
  253.       mat.yy = -1;
  254.     gs_setmatrix(pgs, &mat);
  255.     gs_makepattern(pcc, &pat, &mat, pgs, mem);
  256.     gs_setmatrix(pgs, &smat);
  257.     return 0;
  258. }
  259. private int
  260. bitmap_PaintProc(const gs_client_color *pcolor, gs_state *pgs)
  261. {    gs_image_enum *pen =
  262.       gs_image_enum_alloc(gs_state_memory(pgs), "bitmap_PaintProc");
  263.     const gs_client_pattern *ppat = gs_getpattern(pcolor);
  264.     const byte *dp = ppat->client_data;
  265.     gs_image_t image;
  266.     int n;
  267.     uint nbytes, raster, used;
  268.  
  269.     if ( ppat->PaintType == 2 )
  270.       gs_image_t_init_mask(&image, true);
  271.     else
  272.       { gs_image_t_init_gray(&image);
  273.         image.Decode[0] = 1.0;
  274.         image.Decode[1] = 0.0;
  275.       }
  276.     image.Width = (int)ppat->XStep;
  277.     image.Height = (int)ppat->YStep;
  278.     raster = bitmap_raster(image.Width);
  279.     nbytes = (image.Width + 7) >> 3;
  280.     gs_image_init(pen, &image, false, pgs);
  281.     for ( n = image.Height; n > 0; dp += raster, --n )
  282.       gs_image_next(pen, dp, nbytes, &used);
  283.     gs_image_cleanup(pen);
  284.     gs_free_object(gs_state_memory(pgs), pen, "bitmap_PaintProc");
  285.     return 0;
  286. }
  287.  
  288. /* ------ Color space implementation ------ */
  289.  
  290. /* Pattern device color types. */
  291. /* We need a masked analogue of each of the non-pattern types, */
  292. /* to handle uncolored patterns. */
  293. /* We use 'masked_fill_rect' instead of 'masked_fill_rectangle' */
  294. /* in order to limit identifier lengths to 32 characters. */
  295. private dev_color_proc_load(gx_dc_pattern_load);
  296. private dev_color_proc_fill_rectangle(gx_dc_pattern_fill_rectangle);
  297. private struct_proc_enum_ptrs(dc_pattern_enum_ptrs);
  298. private struct_proc_reloc_ptrs(dc_pattern_reloc_ptrs);
  299. private dev_color_proc_load(gx_dc_pure_masked_load);
  300. private dev_color_proc_fill_rectangle(gx_dc_pure_masked_fill_rect);
  301. private struct_proc_enum_ptrs(dc_masked_enum_ptrs);
  302. private struct_proc_reloc_ptrs(dc_masked_reloc_ptrs);
  303. private dev_color_proc_load(gx_dc_binary_masked_load);
  304. private dev_color_proc_fill_rectangle(gx_dc_binary_masked_fill_rect);
  305. private struct_proc_enum_ptrs(dc_binary_masked_enum_ptrs);
  306. private struct_proc_reloc_ptrs(dc_binary_masked_reloc_ptrs);
  307. private dev_color_proc_load(gx_dc_colored_masked_load);
  308. private dev_color_proc_fill_rectangle(gx_dc_colored_masked_fill_rect);
  309. /* The device color types are exported for gxpcmap.c. */
  310. const gx_device_color_procs
  311.   gx_dc_pattern =
  312.     { gx_dc_pattern_load, gx_dc_pattern_fill_rectangle,
  313.       gx_dc_default_fill_masked,
  314.       dc_pattern_enum_ptrs, dc_pattern_reloc_ptrs
  315.     },
  316.   gx_dc_pure_masked =
  317.     { gx_dc_pure_masked_load, gx_dc_pure_masked_fill_rect,
  318.       gx_dc_default_fill_masked,
  319.       dc_masked_enum_ptrs, dc_masked_reloc_ptrs
  320.     },
  321.   gx_dc_binary_masked =
  322.     { gx_dc_binary_masked_load, gx_dc_binary_masked_fill_rect,
  323.       gx_dc_default_fill_masked,
  324.       dc_binary_masked_enum_ptrs, dc_binary_masked_reloc_ptrs
  325.     },
  326.   gx_dc_colored_masked =
  327.     { gx_dc_colored_masked_load, gx_dc_colored_masked_fill_rect,
  328.       gx_dc_default_fill_masked,
  329.       dc_masked_enum_ptrs, dc_masked_reloc_ptrs
  330.     };
  331. #undef gx_dc_type_pattern
  332. const gx_device_color_procs _ds *gx_dc_type_pattern = &gx_dc_pattern;
  333. #define gx_dc_type_pattern (&gx_dc_pattern)
  334. /* GC procedures */
  335. #define cptr ((gx_device_color *)vptr)
  336. private ENUM_PTRS_BEGIN(dc_pattern_enum_ptrs) {
  337.     return dc_masked_enum_ptrs(vptr, size, index - 1, pep);
  338.     }
  339.     case 0:
  340.     {    gx_color_tile *tile = cptr->colors.pattern.p_tile;
  341.         ENUM_RETURN((tile == 0 ? tile : tile - tile->index));
  342.     }
  343. ENUM_PTRS_END
  344. private RELOC_PTRS_BEGIN(dc_pattern_reloc_ptrs) {
  345.     gx_color_tile *tile = cptr->colors.pattern.p_tile;
  346.     if ( tile != 0 )
  347.       {    uint index = tile->index;
  348.         RELOC_TYPED_OFFSET_PTR(gx_device_color, colors.pattern.p_tile, index);
  349.       }
  350.     dc_masked_reloc_ptrs(vptr, size, gcst);
  351. } RELOC_PTRS_END
  352. private ENUM_PTRS_BEGIN(dc_masked_enum_ptrs) ENUM_SUPER(gx_device_color, st_client_color, mask.ccolor, 1);
  353.     case 0:
  354.     {    gx_color_tile *mask = cptr->mask.m_tile;
  355.         ENUM_RETURN((mask == 0 ? mask : mask - mask->index));
  356.     }
  357. ENUM_PTRS_END
  358. private RELOC_PTRS_BEGIN(dc_masked_reloc_ptrs) {
  359.     gx_color_tile *mask = cptr->mask.m_tile;
  360.     RELOC_SUPER(gx_device_color, st_client_color, mask.ccolor);
  361.     if ( mask != 0 )
  362.       {    uint index = mask->index;
  363.         RELOC_TYPED_OFFSET_PTR(gx_device_color, mask.m_tile, index);
  364.       }
  365. } RELOC_PTRS_END
  366. private ENUM_PTRS_BEGIN(dc_binary_masked_enum_ptrs) {
  367.     return (*gx_dc_procs_ht_binary.enum_ptrs)(vptr, size, index - 2, pep);
  368.     }
  369.     case 0:
  370.     case 1:
  371.       return dc_masked_enum_ptrs(vptr, size, index, pep);
  372. ENUM_PTRS_END
  373. private RELOC_PTRS_BEGIN(dc_binary_masked_reloc_ptrs) {
  374.     dc_masked_reloc_ptrs(vptr, size, gcst);
  375.     (*gx_dc_procs_ht_binary.reloc_ptrs)(vptr, size, gcst);
  376. } RELOC_PTRS_END
  377. #undef cptr
  378.  
  379. /* Macros for pattern loading */
  380. #define FINISH_PATTERN_LOAD\
  381.     while ( !gx_pattern_cache_lookup(pdevc, pis, dev, select) )\
  382.      { code = gx_pattern_load(pdevc, pis, dev, select);\
  383.        if ( code < 0 ) break;\
  384.      }\
  385.     return code;
  386.  
  387. /* Ensure that a colored Pattern is loaded in the cache. */
  388. private int
  389. gx_dc_pattern_load(gx_device_color *pdevc, const gs_imager_state *pis,
  390.   gx_device *dev, gs_color_select_t select)
  391. {    int code = 0;
  392.     FINISH_PATTERN_LOAD
  393. }
  394. /* Ensure that an uncolored Pattern is loaded in the cache. */
  395. private int
  396. gx_dc_pure_masked_load(gx_device_color *pdevc, const gs_imager_state *pis,
  397.   gx_device *dev, gs_color_select_t select)
  398. {    int code = (*gx_dc_procs_pure.load)(pdevc, pis, dev, select);
  399.     if ( code < 0 )
  400.       return code;
  401.     FINISH_PATTERN_LOAD
  402. }
  403. private int
  404. gx_dc_binary_masked_load(gx_device_color *pdevc, const gs_imager_state *pis,
  405.   gx_device *dev, gs_color_select_t select)
  406. {    int code = (*gx_dc_procs_ht_binary.load)(pdevc, pis, dev, select);
  407.     if ( code < 0 )
  408.       return code;
  409.     FINISH_PATTERN_LOAD
  410. }
  411. private int
  412. gx_dc_colored_masked_load(gx_device_color *pdevc, const gs_imager_state *pis,
  413.   gx_device *dev, gs_color_select_t select)
  414. {    int code = (*gx_dc_procs_ht_colored.load)(pdevc, pis, dev, select);
  415.     if ( code < 0 )
  416.       return code;
  417.     FINISH_PATTERN_LOAD
  418. }
  419.  
  420. /* Look up a pattern color in the cache. */
  421. bool
  422. gx_pattern_cache_lookup(gx_device_color *pdevc, const gs_imager_state *pis,
  423.   gx_device *dev, gs_color_select_t select)
  424. {    gx_pattern_cache *pcache = pis->pattern_cache;
  425.     gx_bitmap_id id = pdevc->mask.id;
  426.  
  427.     if ( id == gx_no_bitmap_id )
  428.       {    color_set_null_pattern(pdevc);
  429.         return true;
  430.       }
  431.     if ( pcache != 0 )
  432.       { gx_color_tile *ctile = &pcache->tiles[id % pcache->num_tiles];
  433.         if ( ctile->id == id &&
  434.         (pdevc->type != &gx_dc_pattern ||
  435.          ctile->depth == dev->color_info.depth)
  436.            )
  437.           { if ( pdevc->type == &gx_dc_pattern ) /* colored */
  438.           { pdevc->colors.pattern.p_tile = ctile;
  439.             color_set_phase_mod(pdevc, pis->screen_phase[select].x,
  440.                     pis->screen_phase[select].y,
  441.                     ctile->tbits.rep_width,
  442.                     ctile->tbits.rep_height);
  443.           }
  444.         pdevc->mask.m_tile =
  445.           (ctile->tmask.data == 0 ? (gx_color_tile *)0 :
  446.            ctile);
  447.         return true;
  448.           }
  449.       }
  450.     return false;
  451. }
  452.  
  453. #undef FINISH_PATTERN_LOAD
  454.  
  455. /*
  456.  * The filling procedures below disregard the phase in the color.
  457.  * We don't see how this can possibly work, but it does; and if we
  458.  * do take the phase into account, we get incorrect output.
  459.  */
  460.  
  461. /* Macros for filling with a possibly masked pattern. */
  462. #define BEGIN_PATTERN_FILL\
  463.       {    gx_device_tile_clip cdev;\
  464.         gx_device *pcdev;\
  465.         const gx_strip_bitmap *tmask;\
  466.         int code;\
  467. \
  468.         if ( pdevc->mask.m_tile == 0 )    { /* no clipping */\
  469.           code = 0;\
  470.           pcdev = dev;\
  471.         } else {\
  472.           tmask = &pdevc->mask.m_tile->tmask;\
  473.           code = tile_clip_initialize(&cdev, tmask, dev, 0, 0);\
  474.           if ( code < 0 )\
  475.             return code;\
  476.           pcdev = (gx_device *)&cdev;\
  477.         }
  478. #define CLIPPING_FILL (pcdev == (gx_device *)&cdev)
  479. #define END_PATTERN_FILL\
  480.         return code;\
  481.       }
  482.  
  483. /* Macros for filling with non-standard X and Y stepping. */
  484. /* Free variables: x, y, w, h, ptile. */
  485. /* tbits_or_tmask is whichever of tbits and tmask is supplying */
  486. /* the tile size. */
  487. /* This implementation could be sped up considerably! */
  488. #define BEGIN_STEPS(tbits_or_tmask)\
  489.       {    gs_rect bbox, ibbox;\
  490.         gs_point offset;\
  491.         int x0 = x, x1 = x + w, y0 = y, y1 = y + h;\
  492.         int w0 = w, h0 = h;\
  493.         int i0, i1, j0, j1, i, j;\
  494. \
  495.         bbox.p.x = x0, bbox.p.y = y0;\
  496.         bbox.q.x = x1, bbox.q.y = y1;\
  497.         gs_bbox_transform_inverse(&bbox, &ptile->step_matrix, &ibbox);\
  498.         if_debug10('T',\
  499.               "[T]x,y=(%d,%d) w,h=(%d,%d) => (%g,%g),(%g,%g), offset=(%g,%g)\n",\
  500.               x, y, w, h,\
  501.               ibbox.p.x, ibbox.p.y, ibbox.q.x, ibbox.q.y,\
  502.               ptile->step_matrix.tx, ptile->step_matrix.ty);\
  503.         offset.x = ptile->step_matrix.tx;\
  504.         offset.y = ptile->step_matrix.ty;\
  505.         i0 = (int)floor(ibbox.p.x - ptile->bbox.q.x + 0.000001);\
  506.         i1 = (int)ceil(ibbox.q.x - ptile->bbox.p.x - 0.000001);\
  507.         j0 = (int)floor(ibbox.p.y - ptile->bbox.q.y + 0.000001);\
  508.         j1 = (int)ceil(ibbox.q.y - ptile->bbox.p.y - 0.000001);\
  509.         if_debug4('T', "[T]i=(%d,%d) j=(%d,%d)\n", i0, i1, j0, j1);\
  510.         for ( i = i0; i < i1; i++ )\
  511.           for ( j = j0; j < j1; j++ )\
  512.           {    int xoff, yoff;\
  513.             x = (int)(ptile->step_matrix.xx * i +\
  514.                   ptile->step_matrix.yx * j + offset.x);\
  515.             y = (int)(ptile->step_matrix.xy * i +\
  516.                   ptile->step_matrix.yy * j + offset.y);\
  517.             if_debug4('T', "[T]i=%d j=%d x,y=(%d,%d)", i, j, x, y);\
  518.             w = ptile->tbits_or_tmask.size.x;\
  519.             h = ptile->tbits_or_tmask.size.y;\
  520.             if ( x < x0 ) xoff = x0 - x, x = x0, w -= xoff;\
  521.             else xoff = 0;\
  522.             if ( y < y0 ) yoff = y0 - y, y = y0, h -= yoff;\
  523.             else yoff = 0;\
  524.             if ( x + w > x1 ) w = x1 - x;\
  525.             if ( y + h > y1 ) h = y1 - y;\
  526.             if_debug4('T', "=>(%d,%d) w,h=(%d,%d)\n",\
  527.                   x, y, w, h);\
  528.             if ( w > 0 && h > 0 )\
  529.               {    if ( CLIPPING_FILL )\
  530.                   tile_clip_set_phase(&cdev,\
  531.                               imod(xoff - x, tmask->rep_width),\
  532.                               imod(yoff - y, tmask->rep_height))
  533. #define SOURCE_STEP(src)\
  534.                 (src).sdata = source->sdata + (y - y0) * source->sraster;\
  535.                 (src).sourcex = source->sourcex + (x - x0);\
  536.                 (src).sraster = source->sraster;\
  537.                 (src).id = (w == w0 && h == h0 ?\
  538.                         source->id : gx_no_bitmap_id);\
  539.                 (src).scolors[0] = source->scolors[0];\
  540.                 (src).scolors[1] = source->scolors[1];\
  541.                 (src).use_scolors = source->use_scolors
  542. #define END_STEPS\
  543.               }\
  544.           }\
  545.       }
  546.  
  547. /* Fill a rectangle with a colored Pattern. */
  548. /* Note that we treat this as "texture" for RasterOp. */
  549. private int
  550. gx_dc_pattern_fill_rectangle(const gx_device_color *pdevc, int x, int y,
  551.   int w, int h, gx_device *dev, gs_logical_operation_t lop,
  552.   const gx_rop_source_t *source)
  553. {    gx_color_tile *ptile = pdevc->colors.pattern.p_tile;
  554.     const gx_rop_source_t *rop_source = source;
  555.     gx_rop_source_t no_source;
  556.     gx_strip_bitmap *bits;
  557.  
  558.     if ( ptile == 0 )    /* null pattern */
  559.       return 0;
  560.     if ( rop_source == NULL )
  561.       set_rop_no_source(rop_source, no_source, dev);
  562.     bits = &ptile->tbits;
  563.     BEGIN_PATTERN_FILL
  564.     if ( ptile->is_simple )
  565.       {    int px = imod(-(int)(ptile->step_matrix.tx + 0.5),
  566.                   bits->rep_width);
  567.           int py = imod(-(int)(ptile->step_matrix.ty + 0.5),
  568.                   bits->rep_height);
  569.  
  570.         if ( source == NULL && lop_no_S_is_T(lop) )
  571.           code = (*dev_proc(pcdev, strip_tile_rectangle))(pcdev, bits,
  572.             x, y, w, h,
  573.             gx_no_color_index, gx_no_color_index, px, py);
  574.         else
  575.           code = (*dev_proc(dev, strip_copy_rop))(dev,
  576.             rop_source->sdata, rop_source->sourcex,
  577.             rop_source->sraster, rop_source->id,
  578.             (rop_source->use_scolors ? rop_source->scolors : NULL),
  579.             bits, NULL, x, y, w, h, px, py, lop);
  580.       }
  581.     else
  582.       {    BEGIN_STEPS(tbits);
  583.           { const byte *data = bits->data;
  584.             bool full_transfer = (w == w0 && h == h0);
  585.             gx_bitmap_id source_id =
  586.               (full_transfer ? rop_source->id : gx_no_bitmap_id);
  587.               
  588.             if ( source == NULL && lop_no_S_is_T(lop) )
  589.               code = (*dev_proc(pcdev, copy_color))(pcdev,
  590.                 data + bits->raster * yoff, xoff,
  591.                 bits->raster,
  592.                 (full_transfer ? bits->id : gx_no_bitmap_id),
  593.                 x, y, w, h);
  594.             else
  595.               { gx_strip_bitmap data_tile;
  596.  
  597.             data_tile.data = (byte *)data;    /* actually const */
  598.             data_tile.raster = bits->raster;
  599.             data_tile.size.x = data_tile.rep_width =
  600.               ptile->tbits.size.x;
  601.             data_tile.size.y = data_tile.rep_height =
  602.               ptile->tbits.size.y;
  603.             data_tile.id = bits->id;
  604.             data_tile.shift = data_tile.rep_shift = 0;
  605.             code = (*dev_proc(dev, strip_copy_rop))(dev,
  606.                 rop_source->sdata + (y - y0) * rop_source->sraster,
  607.                 rop_source->sourcex + (x - x0),
  608.                 rop_source->sraster, source_id,
  609.                 (rop_source->use_scolors ?
  610.                  rop_source->scolors : NULL),
  611.                 &data_tile, NULL,
  612.                 x, y, w, h,
  613.                 imod(xoff - x, data_tile.rep_width),
  614.                 imod(yoff - y, data_tile.rep_height),
  615.                 lop);
  616.               }
  617.             if ( code < 0 )
  618.               return code;
  619.           }
  620.         END_STEPS
  621.       }
  622.     END_PATTERN_FILL
  623. }
  624. /* Fill a rectangle with an uncolored Pattern. */
  625. /* Note that we treat this as "texture" for RasterOp. */
  626. private int
  627. gx_dc_pure_masked_fill_rect(const gx_device_color *pdevc, int x, int y,
  628.   int w, int h, gx_device *dev, gs_logical_operation_t lop,
  629.   const gx_rop_source_t *source)
  630. {    gx_color_tile *ptile = pdevc->mask.m_tile;
  631.  
  632.     BEGIN_PATTERN_FILL
  633.     if ( ptile == 0 || ptile->is_simple )
  634.         code = (*gx_dc_procs_pure.fill_rectangle)(pdevc, x, y, w, h,
  635.                     pcdev, lop, source);
  636.     else
  637.       {    BEGIN_STEPS(tmask);
  638.         if ( source == NULL )
  639.           code = (*gx_dc_procs_pure.fill_rectangle)(pdevc, x, y, w, h,
  640.                     pcdev, lop, source);
  641.         else
  642.           { gx_rop_source_t step_source;
  643.             SOURCE_STEP(step_source);
  644.             code = (*gx_dc_procs_pure.fill_rectangle)(pdevc,
  645.                     x, y, w, h,
  646.                     pcdev, lop, &step_source);
  647.           }
  648.         if ( code < 0 )
  649.           return code;
  650.         END_STEPS
  651.       }
  652.     END_PATTERN_FILL
  653. }
  654. private int
  655. gx_dc_binary_masked_fill_rect(const gx_device_color *pdevc, int x, int y,
  656.   int w, int h, gx_device *dev, gs_logical_operation_t lop,
  657.   const gx_rop_source_t *source)
  658. {    gx_color_tile *ptile = pdevc->mask.m_tile;
  659.  
  660.     BEGIN_PATTERN_FILL
  661.     if ( ptile == 0 || ptile->is_simple )
  662.         code = (*gx_dc_procs_ht_binary.fill_rectangle)(pdevc,
  663.                     x, y, w, h,
  664.                     pcdev, lop, source);
  665.     else
  666.       {    BEGIN_STEPS(tmask);
  667.         if ( source == NULL )
  668.           code = (*gx_dc_procs_ht_binary.fill_rectangle)(pdevc,
  669.                     x, y, w, h,
  670.                     pcdev, lop, source);
  671.         else
  672.           { gx_rop_source_t step_source;
  673.             SOURCE_STEP(step_source);
  674.             code = (*gx_dc_procs_ht_binary.fill_rectangle)(pdevc,
  675.                     x, y, w, h,
  676.                     pcdev, lop, &step_source);
  677.           }
  678.         if ( code < 0 )
  679.           return code;
  680.         END_STEPS
  681.       }
  682.     END_PATTERN_FILL
  683. }
  684. private int
  685. gx_dc_colored_masked_fill_rect(const gx_device_color *pdevc, int x, int y,
  686.   int w, int h, gx_device *dev, gs_logical_operation_t lop,
  687.   const gx_rop_source_t *source)
  688. {    gx_color_tile *ptile = pdevc->mask.m_tile;
  689.  
  690.     BEGIN_PATTERN_FILL
  691.     if ( ptile == 0 || ptile->is_simple )
  692.         code = (*gx_dc_procs_ht_colored.fill_rectangle)(pdevc,
  693.                     x, y, w, h,
  694.                     pcdev, lop, source);
  695.     else
  696.       {    BEGIN_STEPS(tmask);
  697.         if ( source == NULL )
  698.           code = (*gx_dc_procs_ht_colored.fill_rectangle)(pdevc,
  699.                     x, y, w, h,
  700.                     pcdev, lop, source);
  701.         else
  702.           { gx_rop_source_t step_source;
  703.             SOURCE_STEP(step_source);
  704.             code = (*gx_dc_procs_ht_colored.fill_rectangle)(pdevc,
  705.                     x, y, w, h,
  706.                     pcdev, lop, &step_source);
  707.           }
  708.         if ( code < 0 )
  709.           return code;
  710.         END_STEPS
  711.       }
  712.     END_PATTERN_FILL
  713. }
  714.  
  715. #undef BEGIN_PATTERN_FILL
  716. #undef END_PATTERN_FILL
  717.  
  718. /* Initialize a Pattern color. */
  719. private void
  720. gx_init_Pattern(gs_client_color *pcc, const gs_color_space *pcs)
  721. {    if ( pcs->params.pattern.has_base_space )
  722.     {    const gs_color_space *pbcs =
  723.           (const gs_color_space *)&pcs->params.pattern.base_space;
  724.         cs_init_color(pcc, pbcs);
  725.     }
  726.     /*pcc->pattern = 0;*/        /* cs_full_init_color handles this */
  727. }
  728.  
  729. /* Install a Pattern color space. */
  730. private int
  731. gx_install_Pattern(gs_color_space *pcs, gs_state *pgs)
  732. {    if ( !pcs->params.pattern.has_base_space )
  733.         return 0;
  734.     return (*pcs->params.pattern.base_space.type->install_cspace)
  735.         ((gs_color_space *)&pcs->params.pattern.base_space, pgs);
  736. }
  737.  
  738. /* Adjust the reference counts for Pattern color spaces or colors. */
  739. private void
  740. gx_adjust_cspace_Pattern(const gs_color_space *pcs, gs_memory_t *mem,
  741.   int delta)
  742. {    if ( pcs->params.pattern.has_base_space )
  743.       (*pcs->params.pattern.base_space.type->adjust_cspace_count)
  744.         ((const gs_color_space *)&pcs->params.pattern.base_space, mem, delta);
  745. }
  746.  
  747. private void
  748. gx_adjust_color_Pattern(const gs_client_color *pcc, const gs_color_space *pcs,
  749.   gs_memory_t *mem, int delta)
  750. {    gs_pattern_instance *pinst = pcc->pattern;
  751.  
  752.     if ( pinst != 0 && (pinst->rc.ref_count += delta) == 0 )
  753.       {    /* Release all the storage associated with the instance. */
  754.         gs_state_free(pinst->saved);
  755.         gs_free_object(mem, pinst, "gx_adjust_color_Pattern");
  756.       }
  757.     if ( pcs->params.pattern.has_base_space )
  758.       (*pcs->params.pattern.base_space.type->adjust_color_count)
  759.         (pcc, (const gs_color_space *)&pcs->params.pattern.base_space,
  760.          mem, delta);
  761. }
  762.  
  763. /* GC procedures */
  764.  
  765. #define pcs ((gs_color_space *)vptr)
  766.  
  767. private ENUM_PTRS_BEGIN_PROC(gx_enum_ptrs_Pattern) {
  768.     if ( !pcs->params.pattern.has_base_space )
  769.       return 0;
  770.     return (*pcs->params.pattern.base_space.type->enum_ptrs)
  771.          (&pcs->params.pattern.base_space,
  772.           sizeof(pcs->params.pattern.base_space), index, pep);
  773. } ENUM_PTRS_END_PROC
  774. private RELOC_PTRS_BEGIN(gx_reloc_ptrs_Pattern) {
  775.     if ( !pcs->params.pattern.has_base_space )
  776.       return;
  777.     (*pcs->params.pattern.base_space.type->reloc_ptrs)
  778.       (&pcs->params.pattern.base_space, sizeof(gs_paint_color_space), gcst);
  779. } RELOC_PTRS_END
  780.  
  781. #undef pcs
  782.